#ifndef __Math_FormulaDataSupport__
#define __Math_FormulaDataSupport__
#include "cocos2d.h"
#include "../Data/LevelData.h"

#define kCocoGhostNodeAdjustEasy 30
#define kCocoGhostNodeAdjustNormal 14
#define kCocoGhostNodeSelectHard 16
class FormulaDataSupport
{
public:
	static Expression getFormulaData(EGameMode mode, int ID, EPlayType type, std::string formula, int bMax, int answerMin, int answerMax)
	{
		Expression expression;
		expression.Question = "1 + 1 = ?";
		expression.Answer = -1;
		expression.NumGhostStart = 0;

		if (formula == "A x B = ?")
		{
			bool plus = (random(0, 1) == 1) ? true : false;
			int answer = random(answerMin, answerMax);
			int a = 0, b = 0;

			if (!plus) // is minus
			{
				b = random(1, bMax);
				a = answer + b;
			}
			else //is plus
			{
				a = random(1, MAX(1, answer - 1));
				b = answer - a;
			}

			char s[100];
			sprintf(s, "%d %c %d = ?", a, plus ? '+' : '-', b);

			expression.Question = s;
			expression.Answer = answer;
			expression.b = b;
			expression.a = a;
			expression.NumGhostStart = getNumGhostStart(mode, ID, type, answer, b);
		}
		else if (formula == "(A x B) x C = ?")
		{
			bool x1 = (random(0, 1) == 1) ? true : false;
			bool x2 = (random(0, 1) == 1) ? true : false;
			int a = 0, b = 0, c = 0;
			int answer = 0;

			if (x1 && x2) // x1 = x2 = add
			{
				a = b = c = random(9, 33);
				answer = (a + b) + c;
			}
			else if (x1 && !x2) // x1 = add, x2 = minus
			{
				// (a + b) - c = ?
				c = random(1, 80);
				int temp = random(c + 1, 99);
				a = random(1, temp - 1);
				b = temp - a;
				answer = (a + b) - c;
			}
			else if (!x1 && x2) // x1 = minus, x2 = add
			{
				answer = random(20, 99);
				c = random(1, answer - random(1, 5));
				int temp = answer - c;
				a = random(temp + 1, 99);
				b = a - temp;
			}
			else if (!x1 && !x2)
			{
				a = random(33, 99);
				b = random(1, a / 2);
				c = random(1, (a - b) - 1);
				answer = (a - b) - c;
			}

			char s[100];
			sprintf(s, "(%d %c %d) %c %d = ?", a, x1 ? '+' : '-', b, x2 ? '+' : '-', c);

			expression.Question = s;
			expression.Answer = answer;
		}
		else if (formula == "A x (B x C) = ?")
		{
			bool x1 = (random(0, 1) == 1) ? true : false;
			bool x2 = (random(0, 1) == 1) ? true : false;
			int a = 0, b = 0, c = 0;
			int answer = 0;

			if (x1 && x2) // x1 = x2 = add
			{
				a = b = c = random(9, 33);
				answer = a + (b + c);
			}
			else if (x1 && !x2) // x1 = add, x2 = minus
			{
				a = random(9, 66);
				int temp = random(1, 33);
				b = random(33, 99);
				c = b - temp;
				answer = a + (b - c);
			}
			else if (!x1 && x2) // x1 = minus, x2 = add
			{
				a = random(66, 99);
				int temp = random(33, 65);
				b = random(10, temp / 2);
				c = temp - b;
				answer = a - (b + c);
			}
			else if (!x1 && !x2)
			{
				b = random(66, 99);
				int temp = random(10, 33);
				c = b - temp;
				a = random(temp + 1, 99);
				answer = a - (b - c);
			}

			char s[100];
			sprintf(s, "%d %c (%d %c %d) = ?", a, x1 ? '+' : '-', b, x2 ? '+' : '-', c);

			expression.Question = s;
			expression.Answer = answer;
		}

		return expression;
	}

	//Only for adjust play, easy and normal mode
	static int getNumGhostStart(EGameMode mode, int ID, EPlayType type, int answer, int b)
	{
		int result = 0;

		if (type == EPlayType::kType_Adjust)
		{
			switch (mode)
			{
			case kMode_Normal:
				result = answer + b;
				break;
			case kMode_Easy:
				result = answer + b;
				break;
			default:
				break;
			}
		}
		return result;
	}

};

#endif